package com.bfb.helpers;

import java.util.ArrayList;

import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.util.Log;

/**
 * This class holds the data for the current mobile user for easy access and to
 * avoid multiple server calls throughout the application run
 * 
 * @author Rana
 * 
 */
public class User {
	private String facebookId = "";
	String androidId = "";

	SharedPreferences mpref;
	final static String neverShowKey = "neverShow";
	final static String allowFBAccessKey = "allowFB";
	final static String hateIngsListKey = "hateIngs";

	private ArrayList<Dish> favoriteDishes = new ArrayList<Dish>();
	private boolean firstTimeToEdit = true;
	private Bitmap profilePic;
	private String userName = "N\\A"; // The default user name is N\A

	/**
	 * The constructor for the User object. Takes an id and sharedPreferences
	 * objects as parameters
	 * 
	 * @param androidId
	 * @param mpref
	 */
	public User(String androidId, SharedPreferences mpref) {
		this.androidId = androidId;
		this.mpref = mpref;
	}

	/**
	 * Returns this user's list of favorite dishes
	 * 
	 * @return {@link ArrayList}
	 */
	public ArrayList<Dish> getFavoriteDishes() {
		return favoriteDishes;
	}

	/**
	 * Sets the user's facebookId with the given string
	 * 
	 * @param id
	 */
	public void setFacebookId(String id) {
		facebookId = id;
	}

	/**
	 * Returns this user's facebook id
	 * 
	 * @return {@link String}
	 */
	public String getFacebookId() {
		return facebookId;
	}

	/**
	 * Given a dish key return true if this dish is in the user's list of
	 * favorite dishes and false otherwise
	 * 
	 * @param dishKey
	 * @return {@link Boolean}
	 */
	public Boolean likesDishByKey(String dishKey) {
		for (int i = 0; i < favoriteDishes.size(); i++) {
			if (favoriteDishes.get(i).getDishKey().equals(dishKey))
				return true;
		}
		return false;
	}

	/**
	 * Given a bitmap sets this user's profile picture
	 * 
	 * @param bitmap
	 */
	public void setProfilePicture(Bitmap bitmap) {
		profilePic = bitmap;
	}

	/**
	 * Returns a bitmap representing the user's profile picture
	 * 
	 * @return {@link Bitmap}
	 */
	public Bitmap getProfilePicture() {
		return profilePic;
	}

	/**
	 * Sets this user's name using the given string
	 * 
	 * @param name
	 */
	public void setUserName(String name) {
		userName = name;
	}

	/**
	 * Returns this user's user name. In case the username is null or an empty
	 * string the string "N\\A" is returned
	 * 
	 * @return {@link String}
	 */
	public String getUserName() {
		if (userName == null || userName.length() == 0)
			return "N\\A";
		return userName;
	}

	/**
	 * Sets the attribute firstTimeToEdit using the boolean value given
	 * 
	 * @param newValue
	 */
	public void setFirstTimeToEdit(Boolean newValue) {
		firstTimeToEdit = newValue;
	}

	/**
	 * Returns the value of the attribute firstTimeToEdit
	 * 
	 * @return {@link Boolean}
	 */
	public Boolean getfirstTimeToEdit() {
		return firstTimeToEdit;
	}

	/**
	 * Given the name, key and image of the dish, it adds a dish to the user's
	 * list of favorite dishes only if it was not there already. This method
	 * returns true if the new dish is added to the list and false otherwise
	 * 
	 * @param dishName
	 * @param dishKey
	 * @param dishImage
	 * @return {@link Boolean}
	 */
	public boolean addToFavorites(String dishName, String dishKey,
			Bitmap dishImage) {

		Dish dish = new Dish(dishImage, dishName, dishKey);
		if (!favoriteDishes.contains(dish)) {
			favoriteDishes.add(dish);
			return true;
		}
		return false;
	}

	/**
	 * Returns the value of the neverShow attribute
	 * 
	 * @return boolean
	 */
	public boolean getNeverShow() {
		return mpref.getBoolean(neverShowKey, false);
	}

	/**
	 * Returns the list of ingredients the user hates from the sharedPreferences
	 * object
	 * 
	 * @return {@link ArrayList}
	 */
	public ArrayList<String> getHateIngs() {
		String ingsList = mpref.getString(hateIngsListKey, "");

		ArrayList<String> result = ListUtilities.stringToList(ingsList);

		Log.e("user hate list", result.toString());
		return result;
	}

	/**
	 * Returns whether facebook access is enabled or not from the
	 * sharedPreferences object
	 * 
	 * @return boolean
	 */
	public boolean getAllowFB() {
		return mpref.getBoolean(allowFBAccessKey, false);
	}

	/**
	 * Sets the never show attribute in the sharedPreferences
	 * 
	 * @param newValue
	 */
	public void setNeverShow(Boolean newValue) {
		SharedPreferences.Editor prefEditor = mpref.edit();
		prefEditor.putBoolean(neverShowKey, newValue);
		prefEditor.commit();
	}

	/**
	 * Sets the allow facebook attribute in the sharedPreferences
	 * 
	 * @param newValue
	 */

	public void setAllowFB(Boolean newValue) {
		SharedPreferences.Editor prefEditor = mpref.edit();
		prefEditor.putBoolean(allowFBAccessKey, newValue);
		prefEditor.commit();
	}

	/**
	 * Sets the list of ingredients the user hates in the sharedPreferences
	 * 
	 * @param newValue
	 */

	public void setHateIngs(ArrayList<String> ingsList) {
		String ingsString = ListUtilities.listToString(ingsList);
		SharedPreferences.Editor prefEditor = mpref.edit();
		prefEditor.putString(hateIngsListKey, ingsString);
		prefEditor.commit();

	}

}
